﻿using System;
using System.Collections.Generic;
using System.Linq;
using JDKD.BMCS.NativeBusiness;
using NutShell.DesignPattern;

namespace JDKD.BMCS.Business
{
        /// <summary>
        /// 表示区域
        /// </summary>
        public class Camera : NativeCamera
        {
                #region 构造函数

                /// <summary>
                /// 初始化摄像机
                /// </summary>
                /// <param name="model">摄像机数据模型</param>
                public Camera(int id)
                {
                        Id = id;
                }

                #endregion 构造函数

                #region 属性

                public int Id { get; private set; }

                /// <summary>
                /// 获取摄像机所属采集卡
                /// </summary>
                private GraphicsCard grphicsCard = null;

                public GraphicsCard GraphicsCard
                {
                        get
                        {
                                if (grphicsCard == null)
                                {
                                        grphicsCard = Singleton<BusinessCache>.Instance.GraphicsCards[Id];
                                }
                                return grphicsCard;
                        }
                }

                private Group group = null;

                public Group Group
                {
                        get
                        {
                                if (group == null)
                                {
                                        group = Singleton<BusinessCache>.Instance.Groups[(Id + 1) / 2];
                                }
                                return group;
                        }
                }

                private Dictionary<int, Channel> channels = null;

                public Dictionary<int, Channel> Channels
                {
                        get
                        {
                                if (channels == null)
                                {
                                        switch (Id)
                                        {
                                                case 1:
                                                        channels = Singleton<BusinessCache>.Instance.Channels.Values.Where(i => i.Id < 5).ToDictionary(j => j.Id);
                                                        break;

                                                case 2:
                                                        channels = Singleton<BusinessCache>.Instance.Channels.Values.Where(i => i.Id < 5).ToDictionary(j => j.Id);
                                                        break;

                                                case 3:
                                                        channels = Singleton<BusinessCache>.Instance.Channels.Values.Where(i => i.Id > 4).ToDictionary(j => j.Id);
                                                        break;

                                                case 4:
                                                        channels = Singleton<BusinessCache>.Instance.Channels.Values.Where(i => i.Id > 4).ToDictionary(j => j.Id);
                                                        break;

                                                default: break;
                                        }
                                }
                                return channels;
                        }
                }

                private Dictionary<Int32, Area> aeras = null;

                public Dictionary<int, Area> Aeras
                {
                        get
                        {
                                if (aeras == null)
                                {
                                        aeras = Singleton<BusinessCache>.Instance.Aeras.Where(i => i.CameraId == Id).ToDictionary(j => j.ChannelId);
                                }
                                return aeras;
                        }
                }

                #endregion 属性

                #region 方法

                public Area HitTest(int x, int y)
                {
                        foreach (var item in Aeras.Values)
                        {
                                if (item.HitTest(x, y))
                                {
                                        return item;
                                }
                        }
                        return null;
                }

                public void UnHitTest()
                {
                        foreach (var item in Aeras.Values)
                        {
                                item.UnHitTest();
                        }
                }

                public void Analyse()
                {
                        foreach (var item in Channels.Values)
                        {
                                item.Analyse();
                        }
                }

                #endregion 方法
        }
}